﻿using KZ.Express.DAL;
using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KZ.Express.Task
{
    public class RoleTask : TaskBase
    {
        new RoleENT _entity;

        public RoleTask()
            : base()
        {
            base._entity = _entity = new RoleENT();
            _entitytype = _entity.GetType();
            _dalBase = new RoleDAL();
        }

        public int Insert(RoleENT role, DataTable objects)
        {
            base._dalBase.BeginTransaction();

            ValidateData(role);
            try
            {
                role.RoleID = _dalBase.Insert(role);

                foreach (DataRow row in objects.Rows)
                {
                    RoleObjectENT roleobject = new RoleObjectENT();
                    roleobject.RoleID = role.RoleID;
                    roleobject.ObjectID = Utility.IntParse(row["ObjectID"]);
                    roleobject.PermissionID = Utility.StringParse(row["PermissionID"]);

                    _dalBase.Insert(roleobject);
                }

                base._dalBase.CommitTransaction();
                return role.RoleID;
            }
            catch (DataAccessException ex)
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_INSERT.ToString());
            }
        }

        public int Update(RoleENT role, DataTable objects)
        {
            ValidateData(role);
            try
            {
                base._dalBase.BeginTransaction();

                _dalBase.Update(role);

                foreach (DataRow row in objects.Rows)
                {
                    RoleObjectENT roleobject = new RoleObjectENT();
                    roleobject.RoleID = role.RoleID;
                    roleobject.ObjectID = Utility.IntParse(row["ObjectID"]);
                    roleobject.PermissionID = Utility.StringParse(row["PermissionID"]);

                    _dalBase.Update(roleobject);
                }

                base._dalBase.CommitTransaction();
                return role.RoleID;
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_INSERT.ToString());
            }
        }

        public override void Delete(string content)
        {
            _dalBase.BeginTransaction();

            try
            {
                (_dalBase as RoleDAL).Delete(content);
                _dalBase.CommitTransaction();
            }
            catch
            {
                _dalBase.RollbackTransaction();
                throw;
            }
        }
    }
}
